import java.util.HashMap;

public class HashBuck {

    static class Node {
        public int key;
        public int val;
        public Node next;

        public Node(int key, int val) {
            this.key = key;
            this.val = val;
        }
    }
    public Node[] array = new Node[10];
    public int usedsize;
    public static final double DEFAULT_LOAD_FACTOR = 0.75f;
    public void push(int key,int val){
        //找到要插入的下标
        int index = key % array.length;
        //判断是否已经存在
        Node cur = array[index];
        while (cur != null){
            if(cur.key == key){
                cur.val = val;
                return;
            }
            cur = cur.next;
        }
        //插入
        Node node = new Node(key,val);
        node.next = array[index];
        array[index] = node;
        usedsize++;
        //负载因子调节
        if(doLoadFactor() >= DEFAULT_LOAD_FACTOR) {
            resize();
        }
    }
    private double doLoadFactor() {
        return usedsize*1.0 / array.length;
    }
    private void resize() {
        Node[] newArray = new Node[2*array.length];

        for (int i = 0;i < array.length; i++){
            Node cur = array[i];
            while (cur != null){
                int index = cur.key % newArray.length;
                Node curN = cur.next;
                cur.next = newArray[index];
                newArray[index] = cur;
                cur = curN;
            }
        }
        array = newArray;
    }

    public int getVal(int key){
        int index = key % array.length;
        Node cur = array[index];
        while (cur != null){
            if(cur.key == key){
                return cur.val;
            }
            cur = cur.next;
        }
        return -1;
    }

}
